package models

import (
	"fmt"
	"io/ioutil"
	"math/rand"
	"net/http"
	"strings"
	"time"

	"github.com/beego/beego/v2/client/orm"
	beego "github.com/beego/beego/v2/server/web"
)

type Homework struct {
	Id          int       `orm:"column(id);auto"`
	StudentID   int       `orm:"column(student_id);null"`
	StudentSN   string    `orm:"column(student_sn);null"`
	Score       int       `orm:"column(score);null"`
	CreateTime  time.Time `orm:"column(createTime);null"`
	Homework    string    `orm:"column(homework);null"`
	IP          string    `orm:"column(IP);null"`
	StudentName string    `orm:"column(student_name);null"`
}

func (t *Homework) TableName() string {
	return "homework"
}

func init() {
	orm.RegisterModel(new(Homework))
}

func CheckHomeWork(no, save int) {
	ip := "172.14.1.%d"
	o := orm.NewOrm()
	number, _ := beego.AppConfig.Int("scanip")
	client := &http.Client{
		Timeout: time.Duration(rand.Intn(1000)+1000) * time.Millisecond,
	}
	for k := 1; k < 2; k++ {
		fmt.Printf("扫描第%d轮，----------------------------  \n", k)

		for i := number; i < 80; i++ {
			var homework Homework
			apiAddr := fmt.Sprintf(ip, i)
			homework.IP = apiAddr
			var err error

			for _, classmate := range AllClassMates {
				if classmate.IP1 == apiAddr {
					homework.StudentSN = classmate.Sn
					homework.StudentID = classmate.Id
					homework.StudentName = classmate.Name
					break
				}
			}
			time.Sleep(time.Second)
			fmt.Printf("扫描IP:%+15s上的作业：  \n", homework.IP)

			startTime := time.Now()

			err = homework1(apiAddr, client, &homework)

			if err != nil {
				fmt.Printf("检查异常:%v \n\n", err.Error())
				continue
			}
			fmt.Printf(" 		用时：%+10v ", time.Since(startTime).String())
			homework.CreateTime = time.Now()

			if save > 0 {
				_, err = o.Insert(&homework)
				if err != nil {
					fmt.Printf("数据库写入出错，错误是:%s  \n", err.Error())
					continue
				}
			}
			fmt.Printf("  得分%+5d  \n", homework.Score)
			fmt.Println("")
			// if homework.Score > 80 {
			// 	fmt.Printf(" 姓名: %s  ", AllClassMates[homework.StudentSN].Name)
			// }
			//fmt.Printf("内容:%v  \n", homework.Homework)
		}
	}
}

func homework1(apiAddr string, client *http.Client, homework *Homework) error {
	//检查
	req, _ := http.NewRequest("GET", "http://"+apiAddr, nil)

	// 发送请求
	resp, err := client.Do(req)
	if err != nil {
		homework.Homework += err.Error() + "-----"

	} else {
		// 读取响应内容
		body, err := ioutil.ReadAll(resp.Body)
		resp.Body.Close()
		if err != nil {
			homework.Homework += err.Error() + "-----"
		}
		nameZuoye := string(body)
		if strings.Contains(nameZuoye, homework.StudentName) {
			homework.Score += 10
		}
		homework.Homework = nameZuoye + "-----"
	}
	//检查加法
	req, _ = http.NewRequest("GET", "http://"+apiAddr+"/add?a=96&b=4", nil)

	// 发送请求
	resp, err = client.Do(req)
	if err != nil {
		homework.Homework += err.Error() + "-----"
		return err
	} else {
		// 读取响应内容
		body, err := ioutil.ReadAll(resp.Body)
		resp.Body.Close()
		if err != nil {
			homework.Homework += err.Error() + "-----"
			return err
		}
		nameZuoye := string(body)
		if strings.Contains(nameZuoye, "100") {
			homework.Score += 30
		}
		homework.Homework += nameZuoye + "-----"
	}
	// 检查bmi
	req, _ = http.NewRequest("GET", "http://"+apiAddr+"/bmi?weight=75&height=1.75", nil)

	// 发送请求
	resp, err = client.Do(req)
	if err != nil {
		homework.Homework += err.Error() + "-----"
		return err
	} else {

		// 读取响应内容
		body, err := ioutil.ReadAll(resp.Body)
		resp.Body.Close()
		if err != nil {
			homework.Homework += err.Error() + "-----"

		}
		nameZuoye := string(body)
		if len(nameZuoye) > 1 {
			homework.Score += 60
		}
		homework.Homework += nameZuoye + "-----"

	}

	return nil
}
